package com.cn.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;

import com.cn.bean.Provider;
import com.cn.bean.Servicecatalog;
import com.cn.service.ServicecatalogService;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;

public class ServicecatalogAction {
	private Servicecatalog servicecatalog;
	private ServicecatalogService servicecatalogService = null;
	HttpServletResponse response;
	
	
	public Servicecatalog getServicecatalog() {
		return servicecatalog;
	}
	public void setServicecatalog(Servicecatalog servicecatalog) {
		this.servicecatalog = servicecatalog;
	}
	public ServicecatalogService getServicecatalogService() {
		return servicecatalogService;
	}
	public void setServicecatalogService(ServicecatalogService servicecatalogService) {
		this.servicecatalogService = servicecatalogService;
	}
	
	/**
	 * 安卓端获取通过st_name获取sc
	 */
	public void getSCbyST(){
		try {
			List list = servicecatalogService.getSCbyST();
			Gson gson = new Gson();
			String json = gson.toJson(list);
			response = ServletActionContext.getResponse();
			response.setContentType("text/json; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public List listAllCatalog(int id)
	{
		return servicecatalogService.listAllCatalog(id);
	}
	
	public String deleteProviderCatalog()
	{
		int sc_id=servicecatalog.getId();
		Map session = (Map) ActionContext.getContext().get("session");
		Provider provider = (Provider) session.get("provider");
		if(servicecatalogService.deleteProviderCatalog(provider.getId(),sc_id))
			return "success";
		else 
			return "fail";
	}
	
	public String changeProviderCatalog()
	{
		HttpServletRequest request=ServletActionContext.getRequest();
		float price=Float.parseFloat(request.getParameter("price"));
		Map session = (Map) ActionContext.getContext().get("session");
		Provider provider = (Provider) session.get("provider");
		int pro_id=provider.getId();
		int sc_id=servicecatalog.getId();
		if(servicecatalogService.changeProviderCatalog(pro_id,sc_id,price))
		return "success";
		else 
			return "fail";
	}
	
	public void catalogPrice()
	{
		System.out.println("正在更新服务种类市场价格");
		servicecatalogService.catalogPrice();
	}
}
